<?php

/**
 * uniBoard API Help File
 *
 * Light and fast forum solution for web 2.0-like communities.
 *
 * @package		uniBoard
 * @author		Rafał Pitoń
 * @license		http://www.gnu.org/licenses/gpl-3.0.html
 * @since		Version 0.1
 * 
 * ------------------------------------------------------------------------
 * 
 * $Date: 2010-09-08 03:38:35 +0000 (Wed, 08 Sep 2010) $
 * $Revision: 60 $
 * $Author: rafio.xudb@gmail.com $
 */

// ------------------------------------------------------------------------

class api_help_file extends class_api{
	
	/**
	 * Main class pointer
	 *
	 * @var object
	 */
	
	private $appCore				= NULL;
	
	/**
	 * File ID
	 *
	 * @var integer
	 */
	
	public $file_id 				= 0;
	
	/**
	 * File Parent
	 *
	 * @var integer
	 */
	
	public $file_parent 			= 0;
	
	/**
	 * File Position
	 *
	 * @var integer
	 */
	
	public $file_pos				= 0;
	
	/**
	 * File Name
	 *
	 * @var string
	 */
	
	public $file_name				= '';
	
	/**
	 * File Text
	 *
	 * @var string
	 */
	
	public $file_text				= '';
					
	// ------------------------------------------------------------------------

	/**
	 * Initialise Api
	 *
	 * @param object $appCore
	 * @param integer $target_id
	 */
	
	function __construct( $appCore, $target_id = 0){
					
		// Store appCore pointer
		$this -> appCore = $appCore;
	
		// Store File ID
		$this -> file_id = (integer) $target_id;
		
		// Autoload If File ID is bigger than zero
		if ( $target_id > 0)
		{
			// Make select
			$appCore -> db -> query( 'SELECT *
			FROM ' . DB_PREFIX . 'help_files
			WHERE file_id = \'' . $this -> file_id . '\'
			LIMIT 1');
			
			// Data found
			if ( $file_result = $appCore -> db -> fetch_assoc())
			{
				$this -> data_loaded		= true;
				
				$this -> file_parent		= $file_result['file_parent'];
				$this -> file_pos			= $file_result['file_pos'];
				$this -> file_name			= $file_result['file_name'];
				$this -> file_text	 		= $file_result['file_text'];
			}
			else
			{
				//give action an error
				$this -> setError( $appCore -> lang['acp_help_file_not_found']);
			}
			
			// Free result
			$appCore -> db -> freeResult();
		}
		
	}
	
	// ------------------------------------------------------------------------

	/**
	 * Save API data to DB
	 *
	 */
	
	function saveData(){
		
		// Do mysql query only when we have changes, and no errors
		if ( !empty( $this -> changes) && !$this -> error)
		{	
			// Insert?
			if ( $this -> file_id == 0)
			{
				// Insert new record
				$this -> appCore -> db -> insert( $this -> changes, 'help_files');
			
				// Store new crawler id
				$this -> crawler_id = $this -> appCore -> db -> insertId();
			}
			else
			{
				// Update record
				$this -> appCore -> db -> update( $this -> changes, 'help_files', 'file_id = \'' . $this -> file_id . '\'');
			}
		
			// Flush cache
			class_cache::flushCache( 'help_files');
		}
		
	}
	
	// ------------------------------------------------------------------------

	/**
	 * Set file_parent
	 *
	 * @param integer $new_value
	 */
	
	function setFileParent( $new_value){
		
		// Store in object
		$this -> file_parent = $new_value;
		
		// Add to made changes
		$this -> changes['file_parent'] = (integer) $this -> file_parent;
	
	}
	
	// ------------------------------------------------------------------------

	/**
	 * Set file_pos
	 *
	 * @param integer $new_value
	 */
	
	function setFilePos( $new_value){
		
		// Store in object
		$this -> file_pos = $new_value;
		
		// Add to made changes
		$this -> changes['file_pos'] = (integer) $this -> file_pos;
	
	}
	
	// ------------------------------------------------------------------------

	/**
	 * Set file_name
	 *
	 * @param string $new_value
	 */
	
	function setFileName( $new_value){
		
		if ( isset( $new_value[0]))
		{
			// Store in object
			$this -> file_name = $new_value;
			
			// Check max lenght
			if ( mb_strlen( $new_value) > 255)
			{
				// Set error
				$this -> setError( $this -> appCore -> lang['acp_help_file_name_too_long']);
			}
			else
			{
				// Add to made changes
				$this -> changes['file_name'] = $this -> appCore -> db -> strEscape( $this -> file_name);
			}
		}
		else
		{
			// Set error
			$this -> setError( $this -> appCore -> lang['acp_help_file_name_empty']);
		}
		
	}
	
	// ------------------------------------------------------------------------

	/**
	 * Set file_text
	 *
	 * @param string $new_value
	 */
	
	function setFileText( $new_value){
		
		if ( isset( $new_value[0]))
		{
			// Store in object
			$this -> file_text = $new_value;
			
			// Add to made changes
			$this -> changes['file_text'] = $this -> appCore -> db -> strEscape( $this -> file_text);
		}
		else
		{
			// Set error
			$this -> setError( $this -> appCore -> lang['acp_help_file_text_empty']);
		}
		
	}
	
}